
<script type="text/javascript">
    $(document).ready(function(){
        $('#advance_search').click(function(){
            if($(this).next().css("height") == '0px'){
                var top = $(this).next().find('.last_line').position().top;
                var panel = $(this).next();
                $(this).next().animate({"height" : top + "px"},500,function(){
                    panel.css("overflow","visible");
                    panel.css("height", "auto");
                });
            }
            else{
                $(this).next().css("overflow","hidden");
                $(this).next().animate({"height" : "0px"},500);
            }
        });

        $('#bt_search').click(function(){
            var keyword = $('#tb_search').val();
            if(keyword.length < 3 && $('.advance_search_panel').css('height') == '0px'){
                return;
            }
            var aParams = {};
            if($('.advance_search_panel').css('height') != '0px'){
                aParams = getParams();
            }
            
            aParams.keyword = keyword;
            aParams.typeSearch = $('#cb_type_search').val();
            $.post('~search/search', aParams , function(data){
                $('#result_header').css('display','block');
                $('.result_panel').html(data);
                $('.lb_keyword').html(keyword);
                var count = $('.result_panel').find('.item').length;
                if($('#cb_type_search').val() == 'All'){
                    count = $('.result_panel').find('.result_result').length;
                }
                $('.lb_number').html(count);
            })
        });
        $('#bt_add_condition').click(function(){
            var value = $('#cb_search_condition').val();
            $('#' + value).insertBefore($('#add_condition_panel')); 
            $('#' + value).fadeIn();
        });

        $('#cb_type_price').change(function(){
            if($(this).val().indexOf('FROM') != -1 ){
                $('#price_type_from_to').css('display','inline-block');
                $('#tb_big_small').css('display','none');
            }
            else{
                $('#price_type_from_to').css('display','none');
                $('#tb_big_small').css('display','inline-block');
            }
        });
        
        $('.remove_condition').click(function(){
            $(this).closest('.condition_item').fadeOut(); 
        });
        $('#cb_type_search').change(function(){
            if($(this).val() == 'Product'){
                $('#advance_search').css('display','block');
                $('.advance_search_panel').css('display','block');
            } 
            else{
                $('#advance_search').css('display','none');
                $('.advance_search_panel').css('display','none');
            }
        });
        function getParams(){
            var sPriceCondition = '';
            if($('#price_condition').is(':visible')){
                if($('#cb_type_price').val().indexOf('FROM') != -1){
                    if($('#tb_price_from').val() != '' && $('#tb_price_to').val() != ''){
                        sPriceCondition = "FILTER(?price >= " + $('#tb_price_from').val() + " && ?price <= " + $('#tb_price_to').val() + ").";
                    }
                    else{

                    }
                }
                else if($('#cb_type_price').val().indexOf('BIGER') != -1){
                    if($('#tb_big_small').val() != ''){
                        sPriceCondition = "FILTER(?price >= " + $('#tb_big_small').val() + ").";
                    }
                    else{

                    }
                }
                else if($('#cb_type_price').val().indexOf('SMALLER') != -1){
                    if($('#tb_big_small').val() != ''){
                        sPriceCondition = "FILTER(?price <= " + $('#tb_big_small').val() + ").";
                    }
                }
            } 
            var sLocationCondition = '';
            if($('#location_condition').is(':visible')){
                if($('#cb_location').val() != ''){
                    sLocationCondition = "FILTER(REGEX(?location,'" + $('#cb_location').val() + "','i')).";
                }
            }

            var sManuFacturer = '';
            if($('#manufacturer_condition').is(':visible')){
                if($('#cb_manufacturer').val() != ''){
                    sManuFacturer = "FILTER(?manufacturer = '" + $("#cb_manufacturer").val() + "').";
                }
            }

            var sUserCondition = '';
            if($('#user_condition').is(':visible')){
                if($('#tb_user_condition').val() != ''){
                    sUserCondition = "FILTER(REGEX(?ownUser,'" + $("#tb_user_condition").val() + "','i')).";
                }
            }

            var sCategoryCondition = '';
            if($('#category_condition').is(':visible')){
                if($('#category_uri').val() != ''){
                    sCategoryCondition = "FILTER(?categoryUri = '" + $("#category_uri").val() + "').";
                }
            }
                        
            var aParams = {};
            if(sPriceCondition != ''){
                aParams.sPriceCondition = sPriceCondition;
            }
            if(sLocationCondition != ''){
                aParams.sLocationCondition = sLocationCondition;
            }
            if(sManuFacturer != ''){
                aParams.sManuFacturer = sManuFacturer;
            }
            if(sUserCondition != ''){
                aParams.sUserCondition = sUserCondition;
            }
            if(sCategoryCondition != ''){
                aParams.sCategoryCondition = sCategoryCondition;
            }
            return aParams;
        }
    });
</script>
<div style="padding-top:20px;padding-bottom: 20px;text-align: center;vertical-align:middle;">
    <input id="tb_search" value="<?php if($template->get('sId')){echo $template->get('sId');} ?>" type="text" style="margin-right: -6px;width: 400px;padding-right:20px;vertical-align:middle;" placeholder="Enter keyword, least 3 word...">
    <?php
        $aParams = array(
            'id' => 'cb_type_search',
            'width' => '60px',
            'middle' => true,
            'disabled' => true,
            'value' => 'All',
            'title' => 'All',
            'aValue' => array(
                'All','Product','User',
            )
        );
        if($template->get('sId'))
        {
            $aParams['value'] = 'Product';
            $aParams['title'] = 'Product';
        }
        includeBlock('block/customComboboxBlock',$aParams); 
    ?>
    <a href="javascript:void(0);"><img id="bt_search" style="position:relative;left:-8px;border:2px solid #333;vertical-align:middle;" src="<?php echo URL::getLink('image/search'); ?>" alt=""></a>
</div>
<h2 id="advance_search" class="title bright_green_style" style="<?php if($template->get('sId')){echo "display:block;";}else{echo "display:none;";} ?>">ADVANCE SEARCH</h2>
<div class="advance_search_panel" style="display: none;">

    <div id="category_condition" class="condition_item">
        <?php 
            $aParams = array(
                'header' => 'Category',
                'headerWidth' => '100px',
                'width' => '270px',
            );
            includeBlock('block/categoryBlock', $aParams); 
        ?>
        <div class="remove_condition"></div>
    </div>

    <div id="price_condition" class="condition_item">
        <input type="text" value="PRICE" disabled style="width:50px;margin-right:-8px;">
        <?php 
            $aParams = array(
                'id' => 'cb_type_price',
                'value' => 'SMALLER',
                'title' => 'SMALLER',
                'disabled' => true,
                'width' => '80px',
                'aValue' => array(
                    'SMALLER','BIGER','FROM..TO..',
                ),
            );
            includeBlock('block/customComboboxBlock',$aParams); 
        ?>
        <input id="tb_big_small" type="text" value="" style="margin-left:-10px;width:250px;" placeholder="Example: 100">
        <div id="price_type_from_to" style="display: inline-block;display: none;">
            <input value="FROM" disabled type="text" style="width: 45px;margin-left:-10px;">
            <input id="tb_price_from" type="text" style="width: 50px;margin-left:-10px;" placeholder="EX: 10">
            <input value="TO" disabled type="text" style="width: 45px;margin-left:-10px;">
            <input id="tb_price_to" type="text" style="width: 50px;margin-left:-10px;" placeholder="EX: 30">
        </div>
        <div class="remove_condition"></div>
    </div>

    <div id="user_condition" class="condition_item">
        <input type="text" value="USER" disabled style="width:126px;margin-right:-8px;">
        <input id="tb_user_condition" type="text" value="" style="width:300px;" placeholder="Example: nguyen loc ,or nguyenloc@gmail.com">
        <div class="remove_condition"></div>
    </div>

    <div id="manufacturer_condition" class="condition_item">
        <?php 
            $aParams = array(
                'id' => 'cb_manufacturer',
                'width' => '274px',
                'header' => 'Manufacturer',
                'headerWidth' => '100px',
                'stringKey' => 'uri',
                'stringTitle' => 'name',
                'aValue' => ($template->get('aManufacturers') ? $template->get('aManufacturers') : array()),
            );
            includeBlock('block/customComboboxBlock',$aParams); 
        ?>
        <div class="remove_condition"></div>
    </div>

    <div id="location_condition" class="condition_item">
        <?php 
            $aParams = array(
                'id' => 'cb_location',
                'width' => '274px',
                'header' => 'Location',
                'headerWidth' => '100px',
                'stringKey' => 'name',
                'stringTitle' => 'name',
                'aValue' => ($template->get('aLocations') ? $template->get('aLocations') : array()),
            );
            includeBlock('block/customComboboxBlock',$aParams); 
        ?>
        <div class="remove_condition"></div>
    </div>

    <div id="add_condition_panel" style="margin-top:20px;height: 50px;text-align: right;padding-left:20px;padding-right:20px;vertical-align: middle;">
        <?php 
            $aParams = array(
                'id' => 'cb_search_condition',
                'width' => '100px',
                'header' => 'Condition',
                'headerWidth' => '70px',
                'middle' => true,
                'useKey' => true,
                'disabled' => true,
                'aValue' => array(
                    'category_condition' => 'Category',
                    'manufacturer_condition' => 'Manufacturer' ,
                    'user_condition' => 'User',
                    'price_condition' => 'Price',
                    'location_condition' => 'Location',
                )
            );
            includeBlock('block/customComboboxBlock',$aParams); 
        ?>
        <a href="javascript:void(0);"><img id="bt_add_condition" style="vertical-align: middle" src="<?php echo URL::getLink('image/add_condition');?>" alt=""></a>
    </div>

    <div class="last_line" style="clear: both;height:1px;"></div>
</div>
<h2 id="result_header" class="title red_style" style="<?php if($template->get('aProducts')){echo "display:block;";}else{echo "display:none;";} ?>">
    RESULTS SEARCH FOR "
    <em><span class="lb_keyword"><?php if($template->get('sId')){echo $template->get('sId');} ?></span>"</em>
    <span style="float: right;margin-right:20px;"><span class="lb_number"><?php if($template->get('aProducts')){echo count($template->get('aProducts'));} ?></span> results</span>
</h2>
<div class="result_panel">
<?php
    if($template->get('aProducts'))
    {
        $aParams = array(
            'aProducts' => $template->get('aProducts'),
        );
        includeBlock('block/productBlock',$aParams);
    }
?>
</div>